package ce.com.cenewbluesdk.proxy;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;


import java.io.Serializable;
import java.util.ArrayList;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.bluetooth.CEBlueToothBase;
import ce.com.cenewbluesdk.entity.CEDevData;
import ce.com.cenewbluesdk.uitl.CEBlueSharedPreference;


/**
 * Created by Administrator on 2016/11/7 0007.
 */
public abstract class CEBluetoothProxyBase {

    Handler handler;

    protected CEBluetoothProxyBase(Context context) {
        this.context = context;
        initProxy();
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    protected void sendMeg(Message meg) {
        if (handler != null) {
            handler.sendMessage(meg);
        }
    }

    protected Message createMessage(int what, Serializable o) {
        Message message = Message.obtain();
        message.what = what;
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", o);
        message.setData(bundle);
        return message;
    }

    protected Message createMessage(int what, Parcelable o) {
        Message message = Message.obtain();
        message.what = what;
        Bundle bundle = new Bundle();
        bundle.putParcelable("data", o);
        message.setData(bundle);
        return message;
    }

    protected Message createMessage(int what, ArrayList<Parcelable> ss) {
        Message message = Message.obtain();
        message.what = what;
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("data", ss);
        message.setData(bundle);
        return message;
    }

    protected Message createMessage(int what, int arg1) {
        Message message = Message.obtain();
        message.arg1 = arg1;
        message.what = what;
        return message;
    }

    protected Message createMessage(String what, int arg1) {
        return createMessage(what.hashCode(), arg1);
    }

    protected Message createMessage(String what, Parcelable o) {
        return createMessage(what.hashCode(), o);
    }

    protected Message createMessage(String what, ArrayList<Parcelable> ss) {
        return createMessage(what.hashCode(), ss);
    }
    protected Message createMessage(String what, Serializable o) {
        return createMessage(what.hashCode(), o);
    }

    /**
     * 数据发送成功
     *
     * @param devData
     */
    protected abstract void dataSendSucceed(CEDevData devData);

    /**
     * 数据发送失败
     *
     * @param devData
     */
    protected abstract void DataSendFailed(CEDevData devData);

    /**
     * 接收到数据
     *
     * @param devData
     */
    protected abstract void receiptData(CEDevData devData);

    /**
     * 蓝牙状态改变
     *
     * @param state
     */
    protected abstract void blueToothConnectStateChange(int state);

    /**
     * 初始化代理
     */
    protected abstract void initProxy();

    /**
     * 这个是获取子代理的类名
     *
     * @return
     */
    protected abstract String getClassName();

    /**
     * @return
     */


    protected void sendData(CEDevData devData) {
        devQueue.push(devData);
    }


    public Context context;
    protected CEBlueToothBase blueTooth;
    protected CEConnectUtil connectUtil;
    protected CEDevQueue devQueue;

    public void connectDev(String mac) {
        connectUtil.connect(mac);
    }

    public void disConnect() {
        connectUtil.disConnect();
    }

    public boolean isConnectOK() {
        return connectUtil.getCurrConnectState() == CEBC.BLUE_CONNECTED;
    }

    public boolean isDisconnect() {
        return connectUtil.getCurrConnectState() == CEBC.BLUE_DISCONNECT;
    }

    public int getConnectTimes() {
        return connectUtil.getConnectTimes();
    }

    public int getCurrConnectState() {
        return connectUtil.getCurrConnectState();
    }

    public String getBlueAddress() {
        return CEBlueSharedPreference.getInstance(context).getBlueToothAddress(getClassName());
    }

    public void setBlueAddress(String address) {
        CEBlueSharedPreference.getInstance(context).setBlueToothAddress(getClassName(), address);
    }


}
